JMU JMU - Department of Computer Science
Help Tools
Systems Programming Style Guide


1 Local Style Guide

You must follow certain guidelines when writing C code.

1.1 Structure

Your code must me structured as follows:
  1. Your code must be appropriately structured into .h files and .c files.
  2. Each .h file must begin and end as follows:
    #ifndef name_h
    #define name_h
    
    
    #endif
    
    where name is the name of the file.
  3. You must always include a makefile that contains all dependencies and can be used to build the project.

1.2 Formatting

Formatting must comply wioth a strict reading of the GNU formatting standard. Specifically:
  1. Lines must 79 characters of less.
  2. Put the open bracket that starts the body of a function in column 1.
  3. Function definitions must start the name of the function in column 1. For example:
    static char *
    concat(char *start, char *end)
    {
    
    }
    
  4. For struct and enum types, put brackets in column 1 unless the whole contents fits on one line.
  5. For control structures, put the brackets on their own lines and indent them two spaces. For example:
    if (x > 0)
      {
        //
      }
    else
      {
        //
      }
    }
    
  6. Split long expressions into multiple lines before operators.

1.3 Names

Variable, function, and file names must comply with a strict reading of the GNU naming standard. In partuicular:
  1. Use descriptive names entities.
  2. Use underscores to separate words in a name.
  3. Use lowercase letters (except in macros and enum constants).

1.4 Comments

Use one of the commenting styles supported by Doxygen. The preferred style is the Javadoc style (which you should be accustomed to).

Every file must include the following comment at/near the top (which, of course, must be true).

// This work complies with the JMU Honor Code.

General descriptive comments (i.e., that would be of use to someone using your code) should be in the .h file (since that is the file that will always be available. Comments about internal details/workings should be in the .c file.

1.5 Practices

  1. Functions that do not have any formal parameters must have a void parameter list, not an empty parameter list.
  2. Do not allocate memory dynamically unless it is necessary (i.e., use statically allocated memory whenever possible).
  3. If possible, when using dynamic memory, have the caller both allocate and free the memory.
  4. Never use strlen(), strcpy() or strcat(), use strnlen(), strncpy() or strncat() instead.
  5. Never use gets(), use fgets() or getchar() instead.

2 Compiling

The GNU compiler is fairly permissive and this can cause problems for beginning (and advanced) C programmers. Hence, you must compile your code with the following options.
-pedantic Issue all warnings demanded by strict ISO
-pedantic-errors Generate an error (rather than a warning) whenever the base standard requires a message
-Wall Enable all warnings about "questionable" constructs
-std=c99 Set the base standard to ISO C99

Some people prefer the C90 standard (which is equivalent to using -ansi) to the C99 standard. Unfortunately, the C90 standard prohibits the use of // comments.

3 Other Style Guides

Different people have different opinions about what is "stylish". If you are interested in such things (and you should be) you might want to take a look at:

Ranade, J. and A. Nash (1992) The Elements of C Programming Style, McGraw Hill , Boston.
(Order from amazon , order from Barnes and Noble , compare at bigwords , compare at CampusBooks4Less , order from Chegg , or search eFollett )

4 Style Humor

As mentioned above, different people have different opinions about what is "stylish". This leads to many fights (and some jokes).

../lectures/comics/Hackles-Style.png
(Courtesy of Hackles)

http://imgs.xkcd.com/comics/code_quality.png
(Courtesy of xkcd)

http://imgs.xkcd.com/comics/code_quality_2.png
(Courtesy of xkcd)

Copyright 2019